﻿using MangoMes.Commont.DapperHelps;
using MangoMes.Commont.Domain.Products;
using MangoMes.Commont.DTOs;
using MangoMes.Commont.Infrastructures.Interfaces;
using MediatR;
using MySqlX.XDevAPI.Common;
using System.Text.RegularExpressions;

namespace MangoMes.Write.API.Applications.ProductManaged
{
    /// <summary>
    /// 物料产品管理 新增
    /// </summary>
    public class ProductManageCreateHandler : IRequestHandler<ProductManageCreateCommon, ResultsDTO>
    {
        #region 仓储
        /// <summary>
        /// 仓储
        /// </summary>
        private readonly IBateRepository<ProductTypes> ProductTypesRepository;
        private readonly IBateRepository<ProductManage> ProductManageRepository;
        private readonly ILogger<ProductManageCreateHandler> logger;
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="productTypesRepository">产品分类</param>
        /// <param name="ProductManageRepository">产品管理</param>
        /// <param name="logger">日志</param>
        public ProductManageCreateHandler(IBateRepository<ProductTypes> productTypesRepository, IBateRepository<ProductManage> ProductManageRepository, ILogger<ProductManageCreateHandler> logger)
        {
            ProductTypesRepository = productTypesRepository;
            this.ProductManageRepository = ProductManageRepository;
            this.logger = logger;
        }
        #endregion

        #region 物料产品管理 新增 
        /// <summary>
        /// 物料产品管理 新增 
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<ResultsDTO> Handle(ProductManageCreateCommon request, CancellationToken cancellationToken)
        {
            //异常处理
            try
            {
                logger.LogInformation("物料产品管理新增成功!");
                //创建DapperHelper对象 用于与数据库进行交互
                DapperHelper dapperHelper = new DapperHelper();
                //初始化一个ResultDTO对象result，并设置Code为 0
                ResultsDTO result = new ResultsDTO() { Code = 0 };
                //创建新实例 aa 通过对象初始化器的方式定义一些属性赋值
                var aa = new ProductManage()
                {
                    ProductManageNode = request.ProductManageNode,
                    ProductManageName = request.ProductManageName,
                    ProductManageNumber = request.ProductManageNumber,
                    Unites = request.Unites,
                    ProductTypeId = request.ProductTypeId,
                    //HeightsAll = request.HeightsAll,
                    IsNo = request.IsNo,
                    Batchs = request.Batchs,
                    Inventory = request.Inventory,
                    Remark = request.Remark,
                   // TwoNumber = request.TwoNumber,
                };

                //连接mySql
                string conn = "server=112.124.69.237;port=3306;uid=root;pwd=No6123666;database=MangoMesDB";

                //sql语句
                string str = $"insert into productmanage values (@ProductManageId,@ProductManageNode,@ProductManageName,@ProductManageNumber,@Unites,@ProductTypeId,@HeightsAll,@IsNo,@Batchs,@Inventory,@Remark,@TwoNumber,@productManageTime)";

                result.Code = await ProductManageRepository.Add(str, aa, conn);
                result.Message = "物料产品管理 新增成功";

                return result;
            }
            catch (Exception)
            {

                throw;
            }
        }
        #endregion

    }
}
